#!/usr/bin/make -f

# debian/rules file for ACE+TAO Debian GNU/Linux packages
# written February 2002 by Ossama Othman <ossama@debian.org>
# Modified August 2003 by Brian Nelson <pyro@debian.org>
# Copyright (C) 2004  Raphael Bossek <bossekr@debian.org>
# Copyright © 2005-2012 Thomas Girard <thomas.g.girard@free.fr>
# Copyright © 2009-2014 Pau Garcia i Quiles <pgquiles@elpauer.org>
# Copyright © 2010-2014 Johnny Willemsen <jwillemsen@remedy.nl>

#export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_BUILD_MAINT_OPTIONS = hardening=+format,+fortify,+stackprotector,+relro,+bindnow
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk
export DEB_CFLAGS_MAINT_APPEND = $(CPPFLAGS)
export DEB_CXXFLAGS_MAINT_APPEND = $(CPPFLAGS)
export DEB_CPPFLAGS_MAINT_APPEND = -g

MWCFLAGS := -type gnuace -noreldefs
DSO_MAKE_FLAG := versioned_so=2
INSTALL_FLAGS := DESTDIR=$(CURDIR)/debian/tmp INSTALL_PREFIX=/usr install_rpath=0 $(DSO_MAKE_FLAG)

MAN1 := debian/ace_netsvcs.1 \
	debian/mpc-ace.1
# Where we install every file before calling dh_install
DT := debian/tmp

DOXYGEN_FILES_TO_REMOVE := -name '*.md5' -o \
                           -name '*.map' -o \
                           -name '*.tag'

# Package names, used when overriding dh_installchangelogs
ALL_PACKAGES := $(shell dh_listpackages)
ALL_ACEXML   := $(filter libacexml%,$(ALL_PACKAGES))

export ACE_ROOT := $(shell pwd)
export MPC_ROOT := $(ACE_ROOT)/MPC

ACE_MAJOR_VERSION := $(shell awk '/ACE_MAJOR_VERSION/ { print $$3 }' ${ACE_ROOT}/ace/Version.h)
ACE_MINOR_VERSION := $(shell awk '/ACE_MINOR_VERSION/ { print $$3 }' ${ACE_ROOT}/ace/Version.h)
ACE_MICRO_VERSION := $(shell awk '/ACE_MICRO_VERSION/ { print $$3 }' ${ACE_ROOT}/ace/Version.h)
ACE_VERSION = ${ACE_MAJOR_VERSION}.${ACE_MINOR_VERSION}.${ACE_MICRO_VERSION}

%:
	dh $@

# Implement our own configure machinery; it will copy Debian specific
# configuration before launching MPC to generate ACE+TAO GNUmakefile
.PHONY: configure
configure: configure-stamp
configure-stamp:
	for fname in debian/*VERSION*; do \
		cp $$fname $$(echo $$fname | \
		sed -e 's/VERSION/${ACE_VERSION}/'); done
	for flname in debian/*.install.in; do \
		sed 's/@DEB_HOST_MULTIARCH@/$(DEB_HOST_MULTIARCH)/g' \
                $$flname > $${flname%.in}; done
	for flname in debian/*.lintian-overrides.in; do \
		sed 's/@ACE_VERSION@/${ACE_VERSION}/g' \
                $$flname > $${flname%.in}; done
	cp debian/platform_macros.GNU $(ACE_ROOT)/include/makeinclude
	cp debian/config.h $(ACE_ROOT)/ace/
	cp debian/default.features $(ACE_ROOT)/bin/MakeProjectCreator/config
	cp debian/ACE-DPKG.mwc $(ACE_ROOT)
	cd $(ACE_ROOT) && $(ACE_ROOT)/bin/mwc.pl $(MWCFLAGS) ACE-DPKG.mwc
	touch $@

override_dh_auto_build-arch: configure-stamp
	$(MAKE) -C $(ACE_ROOT) $(DSO_MAKE_FLAG)

override_dh_auto_build-indep: configure-stamp
	$(MAKE) -C $(ACE_ROOT) $(DSO_MAKE_FLAG)
	cd $(ACE_ROOT) && bin/generate_doxygen.pl -is_release -exclude_tao
	find $(ACE_ROOT)/html $(DOXYGEN_FILES_TO_REMOVE) | xargs -r rm -f

override_dh_auto_install: manpages-stamp
	$(MAKE) -C $(ACE_ROOT) install $(INSTALL_FLAGS)
	mkdir -p $(DT)/usr/share/man/man1
	cp $(ACE_ROOT)/apps/gperf/ace_gperf.1 $(DT)/usr/share/man/man1
#	Relocate .idl and .pidl files
	cd $(DT) && for f in $$(find usr/include -name '*.idl' -o -name '*.pidl') ; do \
	   g=$$(echo $${f%%include*}share/idl/$${f##*usr/include/}) ; \
	   mkdir -p $$(dirname $$g) ; \
	   mv $$f $$g ; \
	   done

override_dh_auto_clean:
	[ -f $(ACE_ROOT)/GNUmakefile ] && $(MAKE) -C $(ACE_ROOT) realclean $(DSO_MAKE_FLAG) || true
	find $(ACE_ROOT) -name 'GNUmakefile*' -o -name '.depend.*' | xargs -r rm
	rm -f $(ACE_ROOT)/include/makeinclude/platform_macros.GNU
	rm -f $(ACE_ROOT)/bin/MakeProjectCreator/config/default.features
	rm -f $(ACE_ROOT)/ace/config.h
	rm -f $(ACE_ROOT)/ACE-DPKG.mwc
	rm -f debian/*.1 debian/mpc-ace.sgml
	rm -f *-stamp
	rm -Rf $(ACE_ROOT)/html/libace-doc \
		$(ACE_ROOT)/html/libacexml-doc \
		$(ACE_ROOT)/doxygen_sqlite3.db

override_dh_compress:
	dh_compress -Xexamples

# ACE+TAO has different changelogs. Use the right one in every package.
override_dh_installchangelogs:
	dh_installchangelogs -pmpc-ace MPC/ChangeLog
	dh_installchangelogs --remaining-packages \
	        ChangeLogs/ACE-$(ACE_MAJOR_VERSION)_$(ACE_MINOR_VERSION)_$(ACE_MICRO_VERSION)

override_dh_fixperms:
ifneq (,$(filter libace-doc, $(shell dh_listpackages)))
	find debian -name '*.pl' | xargs -r sed -i -e '1i#!/usr/bin/perl'
endif
	dh_fixperms

debian/mpc-ace.sgml: MPC/docs/MPC.sgml
	sed -e 's/mpc\.pl/mpc-ace/g' -e 's/mwc\.pl/mwc-ace/g' $< > $@

debian/%.1: debian/%.sgml
	docbook-to-man $< > $@

.PHONY: manpages
manpages: manpages-stamp
manpages-stamp: $(MAN1)
	touch $@
